package 二叉树系列;

public class SumOfLeftLeaves {
    int ans = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        leftLeaves(root);
        if(root==null || (root.right==null && root.left==null)) return 0;
        return ans;
    }
    public void leftLeaves(TreeNode node){
        if(node==null) return;
        leftLeaves(node.left);
        if(node.left==null && node.right==null) ans+=node.val;
        leftLeaves(node.right);
        if(node.right!=null && node.right.left==null && node.right.right==null) ans-=node.right.val;
    }
}
